//
//  AppDelegate.m
//  AddressBook
//
//  Created by mac on 2017/8/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "AppDelegate.h"

#import "TabBarViewController.h"
#import "LoginViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self reachability];
//    [self receiveNotification];
//    [NSThread sleepForTimeInterval:3.0];//设置启动页面时间
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    /*
    BOOL is = NO;
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:LOGIN_SUCCESSFUL] isEqualToString:@"YES"]) {
        is = YES;
    }
    
    TabBarViewController *tabBarVC = [[TabBarViewController alloc] init];
    if (!is) {
        [tabBarVC presentViewController:[LoginViewController new] animated:NO completion:nil];
    }
     */
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:LOGIN_SUCCESSFUL] isEqualToString:@"YES"]) {
        self.window.rootViewController = [TabBarViewController new];
    }else {
        self.window.rootViewController = [LoginViewController new];
    }
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}

-(void)receiveNotification{
    
    //注册登录状态监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginStateChange:)
                                                 name:LOGIN_SUCCESSFUL
                                               object:nil];
    
}

#pragma mark - login changed

- (void)loginStateChange:( NSNotification *)notification
{
    BOOL loginSuccess = [notification.object boolValue];
    UIViewController * rootViewController = nil;
    if (loginSuccess) {//登陆成功加载主窗口控制器
        //加载申请通知的数据
        //        [[ApplyViewController shareController] loadDataSourceFromLocalDB];
        
        TabBarViewController *tabbarController = [[TabBarViewController alloc]init];
        rootViewController = tabbarController;
        
    }
    else{//登陆失败加载登陆页面控制器
        LoginViewController * loginController = [[LoginViewController alloc] init];
        rootViewController = loginController;
        
    }
    self.window.rootViewController = rootViewController;
    
}


- (void)reachability
{
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                
                break;
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                
                
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                
                break;
        }
    }];
    // 3.开始监控
    [mgr startMonitoring];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
